<?php 
	
	/*
		Voodoo Validator
		www.voodoovalidator.com
	*/
	
	/*
		For help: admin@voodoovalidator.com
	*/
	
	/*
		This application was developed originally by 
		Robbie Vasquez - www.robbievasquez.com
		Casey McLaughlin - www.caseymclaughlin.com
	*/
	
	/*
		BIRTHDAY: June 1st, 2009
		LAST MODIFIED: 08/12/2010
		BY: Robbie Vasquez
	*/
	
//================================================
	
	/*
		This script gets the results of the W3 validation
		process. jQuery is needed.
		
		This script validates all web page/css files in
		a directory at once and sets up the files within
		to be refreshed, or re-validated when clicked.
		
		The address of the file it validates
		comes from the $_GET variabled 'url' and
		runs it through the W3 validator, yielding xml
		results. The xml is analyzed for errors and 
		warnings.
		
		The $_GET variable 'file' is
		for displaying the name of the validated file
		in the output.
		
		The $_GET variables 'url' and 'file' are provided
		by the script 'validate_start.js' or
		'validate_continue_script.php'
		
		The xml output provides the number of errors
		and the number of warnings. The script displays
		the validation condition of the file as pulled
		from that xml.
		
		The result is printed on the voodoovalidator.php
		web page.
	*/

//================================================

	// Quit trying after this set time
	set_time_limit(90);

	// These variables come from the javascript
	// in either validate_start.js or validate_continue_script.php
	// The $_GET variable 'file' is not needed, just
	// for display purposes.
	$the_url = $_GET['url'];
	$the_file = $_GET['file'];
?>

<!--
	jQuery is needed in this file. Right now it is running version 1.4
	Even though jQuery is loading to the page voodoovalidator.php
	it is not always affecting this file.  When the first directories
	are clicked, it does not need this.  But when sub-levels of
	directories are clicked they don't connect with the jQuery
	provided by voodoovalidator.php
-->
<script type="text/javascript" src="/voodoo_scripts/jquery.js"></script>
<script type="text/javascript">

	// After a file has been validated, its location on the
	// web page voodoovalidator.php is a div with class 'refresh'
	// When a div of class 'refresh' is clicked this script
	// run the page through the validation process again individually
	$('div.refresh').click(function() {
		$(this).removeClass('refresh');
		
		// The attributes 'id' and 'alt are provided to the div with
		// the php script below or validate_refresh.php.
		// validate_continue.php runs every newly clicked directory
		// through the validation process.
		var the_url = $(this).attr('id');
		var name = $(this).attr('alt');
		
		// Temporarily indicate that the file is being re-validated
		$(this).html('<strong>Refreshing ' + name + '</strong> <br/> <img src="/images/loadbar.gif" alt="loading" />');
		
		// The clicked file is sent through validate_refresh.php
		// which operates in a way similar to this one and yields
		// the new results
		$(this).load('/voodoo_scripts/validate_refresh.php', 'url=' + the_url + '&file=' + name);
		
		// The class 'refresh' is returned so that it can be clicked again
		$(this).addClass('refresh');
	});

</script>

<?php

	// To view the output results on this file alone to analyze what it is doing
	if (isset($_GET['dumpdata']))
		$dump_data = TRUE;
	
	
	// Chosen only these file types are allowed to run through the validator
	$allowed_extensions = array('php', 'html', 'htm', 'asp', 'aspx', 'css', 'shtml', 'phtml');
	
	
	// Separate the file from its extension.
	// File names with a '.' in the name may not operate correctly
	$url_sections = explode('.', $the_url);
	$file_extension = strtolower(trim(array_pop($url_sections)));
	
	
	// If the type of the file is not accepted, indicate
	if ( ! in_array($file_extension, $allowed_extensions))
	{
		echo '<span class="na">' . $the_file . ' cannot be validated.</span>';
		exit();
	}
	
	
	// The validator can do both css and web page files, but not with the same validator.
	
	if($file_extension != 'css')
		$xml_data = file_get_contents('http://validator.w3.org/check?uri=' . $the_url . '&output=soap12&charset=utf-8');
		
	if ($file_extension == 'css')
		$xml_data = file_get_contents('http://jigsaw.w3.org/css-validator/validator?uri=' . $the_url . '&output=soap12&charset=utf-8');
	
	if (isset($dump_data))
	{
		header("Content-type: text/plain");
		echo $xml_data;
		exit();
	}
	
	// An array is needed to hold the number of errors
	$error_matches = array();
	
	// <m:errorcount></m:errorcount> is a piece in the xml output of the W3 validator
	preg_match("/<m\:errorcount>(.*)<\/m\:errorcount>/", $xml_data, $error_matches);
	$warning_matches = array();
	
	// <m:warnings></m:warnings> is a piece in the xml output of the W3 validator
	preg_match("/<m\:warningcount>(.*)<\/m\:warningcount>/", $xml_data, $warning_matches);

	if (isset($error_matches[1]))
		$number_of_errors = $error_matches[1];
	else
		$number_of_errors = 0;
		
	if (isset($warning_matches[1]))
		$number_of_warnings = $warning_matches[1];
	else
		$number_of_warnings = 0;
	
	if ($number_of_errors > 0)
	{
		// If there are errors, this script plans to print the following
		
		if($file_extension != 'css')
			$html_out = '<div class="refresh" id="' . $the_url . '" alt="' . $the_file . '"><div class="errors">' . $the_file . ' has Errors: ' . $number_of_errors . ' - <a href="http://validator.w3.org/check?uri=' . $the_url . '">See the errors</a></div></div>';
			
		if($file_extension == 'css')
			$html_out = '<div class="refresh" id="' . $the_url . '" alt="' . $the_file . '"><div class="errors">' . $the_file . ' has CSS Errors: ' . $number_of_errors . '</div></div>';
	}
		
	if ($number_of_errors == 0)
	{
		// If there are no errors, this script plans to print the following
		// Since this is setting up for refreshing, the div indicating validation
		// must be within a div of class 'refresh'
		
		if($file_extension != 'css')
			$html_out = '<div class="refresh" id="' . $the_url . '" alt="' . $the_file . '"><div class="valid">' . $the_file . ' is Valid! <span class="warnings">There are ' . $number_of_warnings . ' warnings.</span> - <a href="http://validator.w3.org/check?uri=' . $the_url . '">See the results</a></div></div>';
			
		if($file_extension == 'css')
			$html_out = '<div class="refresh" id="' . $the_url . '" alt="' . $the_file . '"><div class="valid">' . $the_file . ' is Valid CSS! <span class="warnings">There are ' . $number_of_warnings . ' warnings.</span></div></div>';
	}
	
	// Print the results onto voodoovalidator.php
	echo $html_out;
	
	unset($the_url);
	unset($the_file);

/* EOF */